package com.reptile.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.reptile.dto.MaterialDTO;
import com.reptile.entity.YsMaterial;
import com.reptile.mapper.YsMaterialMapper;
import com.reptile.service.YsMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 素材接口实现类
 *
 * @author songyafei
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class YsMaterialServiceImpl extends ServiceImpl<YsMaterialMapper, YsMaterial> implements YsMaterialService {


    private final YsMaterialMapper ysMaterialMapper;

    /**
     * 保存素材
     *
     * @param materialDTO
     * @return
     */
    @Override
    public Integer save(MaterialDTO materialDTO) {
        if (Objects.isNull(materialDTO)) {
            throw new BaseException(BaseEnum.DATA_IS_NULL);
        }
        YsMaterial ysMaterial = new YsMaterial();
        BeanUtils.copyProperties(materialDTO, ysMaterial);
        return ysMaterialMapper.insert(ysMaterial);
    }

    /**
     * 查询素材
     *
     * @param materialDTO 素材对象
     * @return
     */
    @Override
    public List<YsMaterial> queryMaterial(MaterialDTO materialDTO) {
        if (Objects.isNull(materialDTO)) {
            throw new BaseException(BaseEnum.FIND_PARAMETER_IS_NULL);
        }
        QueryWrapper<YsMaterial> queryWrapper = new QueryWrapper<>();
        return ysMaterialMapper.selectList(queryWrapper);
    }

    /**
     * 批量导入图片
     * @param materialDTO
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT)
    public Boolean saves(List<MaterialDTO> materialDTO) {
        if (CollectionUtils.isEmpty(materialDTO)) {
            return false;
        }
        log.info("开始批量导入图片：", JSON.toJSONString(materialDTO));
        try {
            // 进行拆解封装
            List<YsMaterial> newsData = materialDTO.stream().map(item -> {
                YsMaterial ysMaterial = new YsMaterial();
                BeanUtils.copyProperties(item, ysMaterial);
                return ysMaterial;
            }).collect(Collectors.toList());
            this.saveBatch(newsData);
        }catch (Exception e){
            log.error("批量导入图片失败！{}",e);
            throw new BaseException(e);
        }
        log.info("批量导入图片完成！");
        return true;
    }

    /**
     * 删除重复的素材数据接口实现
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT)
    public Boolean deleteRepeatData() {
        List<Long> longs = ysMaterialMapper.queryRepeatData();
        if (CollectionUtils.isEmpty(longs)) {
            log.info("本次未查到需要删除的素材数据！程序结束");
            return true;
        }
        try {
            log.info("本次需要删除的素材数据:{}", JSON.toJSONString(longs));
            return this.removeByIds(longs);
        } catch (Exception e) {
            log.error("删除素材重复数据失败！{}",e);
            throw new BaseException(e);
        }
    }
}
